﻿using Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

namespace WebApplication1
{
    public partial class Checkout : System.Web.UI.Page
    {
        double total = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            // register script for time selection script
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "Action", @"  $(function () {
                        var nowTemp = new Date();
                        var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
                        $('#dpdPick').datetimepicker({

                            minDate: now,
                            minuteStepping: 30

                        });
                    });", true);

            if (!IsPostBack)
                ShowCart(); 
        }
        protected void lsvCart_PreRender(object sender, EventArgs e)
        {
           
                Label lblTot = this.lsvCart.FindControl("lblTotal") as Label;
                if (lblTot != null)
                {

                    lblTot.Text = total.ToString(); ;

                }
            
        }


        
        protected void lsvCart_DataBound(object sender, ListViewItemEventArgs e)
        {

            if (e.Item.ItemType == ListViewItemType.DataItem)
            {

                double price = (double)((Business.Cart)e.Item.DataItem).price;
                double quantity = (double)((Business.Cart)e.Item.DataItem).quantity;

                total = total + Convert.ToDouble(price) * Convert.ToDouble(quantity);

            }
        }

        protected void lsvCart_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "CancelItem")
            {
                long FoodId = Convert.ToInt64(e.CommandArgument.ToString());
                List<Cart> orders = (List<Cart>)Session["orders"];
                var remove = orders.SingleOrDefault(c => c.FoodId == FoodId);
                orders.Remove(remove);
                ShowCart();
            }
        }

        public void ShowCart()
        {

            lsvCart.DataSource = (List<Cart>)Session["orders"];
            lsvCart.DataBind();
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                Business.User userobj = (User)Session["User"];

                TextBox pickuptime = (TextBox)lsvCart.FindControl("dpdPick");
                if (string.IsNullOrEmpty(pickuptime.Text))
                    throw new Exception("Pickup time cannot be empty!");
                DateTime picktime;
                CultureInfo culture;
                DateTimeStyles styles;
                culture = CultureInfo.CreateSpecificCulture("en-US");
                styles = DateTimeStyles.None;

                DateTime.TryParse(pickuptime.Text, culture, styles, out picktime);

                TextBox txtRemarks = (TextBox)lsvCart.FindControl("txtRemarks");
                Order ordering = new Order();
                List<Cart> items = (List<Cart>)Session["orders"];

                string orderId = ordering.ConfirmOrder(items, txtRemarks.Text, picktime, userobj);
                Session.Remove("orders");
                ShowCart();
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-success  alert-dismissible";
                lblMessage.Text = "Order done! Your order ID is " + orderId + ".";
            }
            catch (Exception ex)
            {

                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-danger  alert-dismissible";
                lblMessage.Text = ex.Message;
                return;
            }
          

        }

    }
}